//
//  WBMainController.swift
//  WeiBo
//
//  Created by 安中 on 2020/4/28.
//  Copyright © 2020 az. All rights reserved.
//

import UIKit

class WBMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置tabBar是否透明
        tabBar.isTranslucent = false
        tabBar.tintColor = UIColor.black
        
        //设置tabBarItem的title，以及点击和不点击状态图片
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font : UIFont.systemFont(ofSize: 12)], for: .normal)

        //设置tabBarItem的title的字体颜色和字大小及字体样式
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font : UIFont.systemFont(ofSize: 12), NSAttributedString.Key.foregroundColor : UIColor.orange], for: .selected)
        
        setupChildControllers()
        setupComposeButton()
    }
    
    //MARK: - 编写按钮 撰写微博
    @objc private func composeStatus() {
        print("撰写微博---center")
    }
    
    //MARK: - 私有控件
    private lazy var composeButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "编写"), for: .normal)
//        btn.setTitle("编写", for: .normal)
//        btn.backgroundColor = UIColor.orange
        return btn
    }()
}


//在swift中还可以切分代码 把功能相近的函数 放在一个extension中
//MARK: - 设置界面
extension WBMainController {
    //MARK: - 设置编写按钮
    private func setupComposeButton() {
        tabBar.addSubview(composeButton)
        
        //计算按钮的宽度
        let count = CGFloat(children.count)
        let width = tabBar.bounds.width / count
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * width, dy: 0)
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }
    
    //MARK: - 设置所有自控制器
    private func setupChildControllers() {
        let array = [
            ["clsName": "WBHomeController", "title": "首页", "imageName": "主页"],
            ["clsName": "WBMessageController", "title": "消息", "imageName": "消息"],
            ["clsName": "UIViewController", "title": "编写"],
            ["clsName": "WBDiscoverController", "title": "发现", "imageName": "发现"],
            ["clsName": "WBProfileController", "title": "我的", "imageName": "我的"]
        ]
        
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
    }
    
    //MARK: - 使用字典创建一个子控制器
    private func controller(dict: [String: String]) -> UIViewController {
        //1.取得字典内容
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],

            let cls = NSClassFromString(Bundle.main.nameSpace() + "." + clsName) as? UIViewController.Type
            
            else {
            return UIViewController()
        }
        
        //2.创建视图控制器
        let vc = cls.init()
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName + "-未选中")
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "-选中")?.withRenderingMode(.alwaysOriginal)
        let nav = WBNaviController(rootViewController: vc)
        
        return nav
    }
}
